package com.chc.web.bookcase.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chc.utils.CommonResult;
import com.chc.web.book.entity.Book;
import com.chc.web.book.entity.BookParm;
import com.chc.web.bookcase.entity.BookCase;
import com.chc.web.bookcase.service.BookCaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created with IntelliJ IDEA
 *
 * @Author: chc
 * @Date: 2022/04/21/17:25
 * @Description:
 */
@RestController
@RequestMapping("/api/bookcase")
public class BookCaseController {

    @Autowired
    private BookCaseService bookCaseService;

    /**
     * 图书列表数据
     * */
    @GetMapping("/list")
    public CommonResult getBookCaseList(BookParm parm){
        IPage<BookCase> list = bookCaseService.list(parm);
        return CommonResult.success("查询成功",list);
    }

    /**
     * 新增图书
     * @param book
     * @return
     */
    @PostMapping
    public CommonResult addBookCase(@RequestBody BookCase book){
        boolean save = bookCaseService.save(book);
        if(save){
            return CommonResult.success("新增图书类型成功");
        }
        return CommonResult.error("新增图书类型失败");
    }

    /**
     * 编辑图书
     * @param book
     * @return
     */
    @PutMapping
    public CommonResult editBook(@RequestBody BookCase book){
        boolean save = bookCaseService.updateById(book);
        if(save){
            return CommonResult.success("修改图书类型成功");
        }
        return CommonResult.error("修改图书类型失败");
    }

    /**
     * 删除角色
     * @param bookId
     * @return
     */
    @DeleteMapping("/{id}")
    public CommonResult deleteRole(@PathVariable("id") Long bookId){
        boolean save = bookCaseService.removeById(bookId);
        if(save){
            return CommonResult.success("删除图书类型成功");
        }
        return CommonResult.error("删除图书类型失败");
    }
}
